package sctu.edu.java.year2016.team01.lishuai.day20160606.chapter12_2;

/**
 * Created by 李大帅 on 2016/6/11.
 * 使用字符输入、输出流读取文件，将一段文字加密后存入文件，在后再读取，并将加密前的文件与
 * 加密后的文件输出。。
 */
import java.io.*;
public class SecretExample {

    public void Write1() throws IOException {
        File  file = new File("F:\\java\\hello1.txt");

        Writer out = new FileWriter(file,true);

        String info = "You are my dearest";
        char[] a = info.toCharArray();
        for(int i =0;i<a.length;i++)
        {
            a[i] = (char)(a[i]^'a');
        }
        out.write(a);
        System.out.println("加密前的文件：");
        System.out.println(info);
        out.close();
    }

    public  void reader() throws FileNotFoundException {
        File file = new File("F:\\java\\hello1.txt");
        Reader in = new FileReader(file);
        char[] cs = new char[30];
        int len = -1;
        StringBuffer sb = new StringBuffer();
        try {
            while ((len = in.read(cs))!=-1)
            {
                sb.append(new String(cs,0,len));
            }
            in.close();
            System.out.println("加密后的文件：");
            System.out.println(sb);
        }

        catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws FileNotFoundException {

        SecretExample se = new SecretExample();
        try {
            se.Write1();
        } catch (IOException e) {
            e.printStackTrace();
        }

        se.reader();


    }
}

